package com.foton.logistic.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.foton.logistic.R;
import com.foton.logistic.base.BaseActivity;
import com.foton.logistic.listener.IOnResultListener;
import com.foton.logistic.model.ResultEntity;
import com.foton.logistic.task.DispatchTask;
import com.foton.logistic.task.ShowDialogTask;
import com.foton.logistic.task.TaskConstant;
import com.foton.logistic.util.BaseConstant;
import com.foton.logistic.util.OptionUtil;
import com.foton.logistic.util.net.HttpUtil;
import com.foton.logistic.util.tool.RegexUtil;
import com.foton.logistic.util.tool.StringUtil;

import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by wuhaocong on 2017/9/13.
 */

public class ModifyTelActivity extends BaseActivity {
    @InjectView(R.id.ft_ui_oldphone)
    EditText ftUiOldphone;
    @InjectView(R.id.ft_ui_newphone)
    EditText ftUiNewphone;
    @InjectView(R.id.ft_ui_code)
    EditText ftUiCode;
    private ReceiveBroadcast receiveBroadcast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }
        setTaskTag(getClass().getSimpleName());
        setContentView(R.layout.ft_ui_modify_tel);
        ButterKnife.inject(this);
    }


    @Override
    public void init() {
        setBackLayoutVisibility(View.VISIBLE);
        setTitleText("修改手机号");
        setTitleTextVisibility(View.VISIBLE);
        setConfirmCodeType("1");
        registerBroad();
        ftUiNewphone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setConfirmTel(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void registerBroad() {
        try {
            if (receiveBroadcast == null) {
                receiveBroadcast = new ReceiveBroadcast();
                IntentFilter filter = new IntentFilter();
                filter.addAction(BaseConstant.ACTION_ENBLE);
                this.registerReceiver(receiveBroadcast, filter);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public class ReceiveBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                if (intent.getAction().equals(BaseConstant.ACTION_ENBLE)){
                    int type = intent.getIntExtra(BaseConstant.INTENT_TYPE, 0);
                    if (type == 0) {
                        ftUiNewphone.setEnabled(true);
                    } else if (type == 1) {
                        ftUiNewphone.setEnabled(false);

                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
    @Override
    public void onDestroy() {
        try {
            this.unregisterReceiver(receiveBroadcast);
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }

    @OnClick(R.id.ft_ui_next)
    public void onViewClicked() {
        requestData();
    }

    private void requestData() {
        String Oldphone = ftUiOldphone.getText().toString().trim();
        String Newphone = ftUiNewphone.getText().toString().trim();
        String Code = ftUiCode.getText().toString().trim();
        if (StringUtil.isEmpty(Newphone)) {
            OptionUtil.addToast(this, "手机号码不能为空");
            return;
        } else if (!RegexUtil.checkMobile(Newphone)) {
            OptionUtil.addToast(this, "手机号格式不正确");
            return;
        }else if (Code.length()<=0) {
            OptionUtil.addToast(this, "验证码不能为空");
            return;
        }else if(!Code.equals(smsCode)){
            OptionUtil.addToast(this, "验证码不一致");
            return;
        }
        Map<String, Object> argMap = HttpUtil.getEncryMap(this);
        argMap.put("oldTelphone", Oldphone);
        argMap.put("newTelphone", Newphone);
        argMap.put("smsCode", Code);
        ShowDialogTask task = new ShowDialogTask(this, taskTag, titleText, getString(R.string.committing), true, BaseConstant.changeTelphone, argMap, TaskConstant.POST);
        task.setParseClass(ResultEntity.class);
        task.setiOnResultListener(new IOnResultListener() {
            @Override
            public void onOK(DispatchTask task) {
                OptionUtil.addToast(ModifyTelActivity.this, "更新手机号成功");
                finishSelf();
                Intent intent=new Intent(ModifyTelActivity.this,LoginActivity.class);
                startActivity(intent);
            }

            @Override
            public void onError(DispatchTask task) {
            }

            @Override
            public void onDone(DispatchTask task) {

            }
        });
        task.execute();
     }
    }

